﻿using BLL;
using DevExpress.XtraExport.Helpers;
using DevExpress.XtraGrid.Views.Grid;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace 后整理管理系统.自定义类
{
    public class AccountingHelper
    {
        public static void Reconciliation(GridView gridView, string fieldname)
        {
            if (AccessBLL.CheckAccess("标识打样单对账"))
            {
                try
                {
                    Connect.dbcontext.BeginTran();
                    var selectrow = SelectHelper.GetSelect(gridView);
                    //foreach (DataRow s in selectrow.Rows)
                    //{
                    //    Connect.dbcontext.Updateable<danjumingxitable>().SetColumns(x => x.Reconciliation == true).Where(x => x.ID == s["ID1"].ObjToInt()).ExecuteCommand();
                    //}
                    var wherestring = GetInWhereString(selectrow, "ID1");
                    Connect.dbcontext.Ado.ExecuteCommand($"Update danjumingxitable set Reconciliation= abs(Reconciliation-1) where ID IN {wherestring}");
                    Connect.dbcontext.CommitTran();
                }
                catch (Exception ex)
                {
                    Connect.dbcontext.RollbackTran();
                    Tools.ShowMessageBox.ShowErrorBox("标识打样单已对账的时候发生了错误！" + ex.Message);
                }
            }
        }
        public static string GetInWhereString( DataTable selects,string fieldname)
        {
            var selectdanhaos = new List<string>();
            foreach (DataRow row in selects.Rows)
            {
                selectdanhaos.Add(row[selects.Columns[fieldname]].ToString());
            }
            selectdanhaos = selectdanhaos.Distinct().ToList();
            var sb = string.Empty;
            sb += "(";
            foreach (var item in selectdanhaos)
            {
                sb += "'" + item + "',";
            }
            return sb.Substring(0, sb.Length - 1) + ")";
        }
        ///// <summary>
        ///// 记账
        ///// </summary>
        ///// <param name="gridView"></param>
        //public static void Accounting(GridView gridView, string fieldname)
        //{
        //    try
        //    {
        //        var selects = GridViewHelper.GetSelectTable(gridView);
        //        var selectdanhaos = new List<string>();
        //        foreach (DataRow row in selects.Rows)
        //        {
        //            selectdanhaos.Add(row[selects.Columns[fieldname]].ToString());
        //        }
        //        selectdanhaos = selectdanhaos.Distinct().ToList();
        //        Connect.dbcontext.BeginTransaction();
        //        foreach (var danhao in selectdanhaos)
        //        {
        //            单据BLL.Accounting(danhao);
        //        }
        //        Connect.dbcontext.Commit();
        //    }
        //    catch (Exception ex)
        //    {
        //        Connect.dbcontext.Rollback();
        //        Tools.ShowMessageBox.ShowErrorBox("该单据记账的时候发生了错误：" + ex.Message);
        //    }
        //}
        ///// <summary>
        ///// 反记账
        ///// </summary>
        ///// <param name="gridView"></param>
        //public static void ReverseAccounting(GridView gridView, string fieldname)
        //{
        //    try
        //    {
        //        var selects = GridViewHelper.GetSelectTable(gridView);
        //        var selectdanhaos = new List<string>();
        //        foreach (DataRow row in selects.Rows)
        //        {
        //            selectdanhaos.Add(row[selects.Columns[fieldname]].ToString());
        //        }
        //        selectdanhaos = selectdanhaos.Distinct().ToList();
        //        Connect.dbcontext.BeginTran();
        //        foreach (var danhao in selectdanhaos)
        //        {
        //            单据BLL.ReverseAccounting(danhao);
        //        }
        //        Connect.dbcontext.CommitTran();
        //    }
        //    catch (Exception ex)
        //    {
        //        Connect.dbcontext.RollbackTran();
        //        Tools.ShowMessageBox.ShowErrorBox("该单据反记账的时候发生了错误：" + ex.Message);
        //    }
        //}
    }
}
